define(['app','datetime','pagination','editor','exporting','autocomplete','filesaver', 'fileupload', 'js/service/agupload','js/filter/tree2Arr'], function( app ) {
	'use strict'; 
	//奖罚

	app.controller('Jiangcheng', function($rootScope, $scope, dataFactoryCRM, $q,$http, AgUpload,$timeout) {
		$scope.sectors = {};
		$scope.psearch     = {};
		$rootScope.strhrtemp = null;

		//分页
		$scope.vPage  = {pageAll: 1,pageNow: 1,pageSize: 15,k: 3};	
		$scope.psearch['p'] = $scope.vPage.pageNow;
		$scope.psearch['psize'] = $scope.vPage.pageSize;
		//惩罚缘由
		$scope.cfcause = [{ k: '-1', v: '全部' }, { k: '0', v: '迟到' }, { k: '1', v: '早退' }];

		//惩罚方式
		$scope.cfways = [{ k: '-1', v: '全部' }, { k: '0', v: '现金' }, { k: '1', v: '加班' }];
        /**获取部门信息 */
        $scope.getSector = function() {
            var _url = 'Fine/index';
            dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
                if (_d.rcode) {
                    $scope.sectors = _d.data;
                }
            });
        };

		//首页
		$scope.getDataList  = function() {
			var _url ='Jiangcheng/getHrPunishData?'+$.param($scope.psearch);
			dataFactoryCRM.getDataByAPI(_url).then(function(data){
                if (data.rcode) {
                    $scope.sdata_list = data.data;
                    $scope.vPage.pageSize = data.psize;
                    $scope.vPage.pageAll = data.pall;
                    $scope.vPage.pageNow = data.p;
                } else {
                    $scope.vPage.pageAll = 0;
                }
			})
    	}
		//初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
	    window.loadClientCacheToScope($scope, function(){
	    	$scope.getDataList();
	    });

	    //列表搜索
	    $scope.search = function(){
	    	$scope.getDataList();
	    }


        //初始化用户数据
		$scope.userlist  = function( skey ) {
			var _url ='Jiangcheng/getUserList?key=' + skey;
			dataFactoryCRM.getDataByAPI(_url).then(function(data){
                if (data.rcode) {
                    $scope.uslist = data.data;
                    var tval = $('#addpunist_seluser').val();
                    if ( tval != null ) {
                    	if ($rootScope.strhrtemp != null) {
                			$rootScope.strhrtemp += ',' + tval;
                    	}else{
                    		$rootScope.strhrtemp = tval;
                    	}
                    }
                }
			})
    	}
        //监听select事件
        $('#frm-addpunish').bind('input propertychange', function() { 
            var vtext = $("#frm-addpunish .sizer").text();
            if ( vtext != null && vtext.length > 0) {
            	$scope.userlist(vtext);
            }
        });

        $scope.checkGetUser = function(){
        	var nseluser = $('#addpunist_seluser').val();
        	if ($rootScope.strhrtemp.length > 0 || nseluser != null) {
        		return true;
        	}else{
        		return false;
        	}
        }

		//添加记录
		$scope.addShow         = function(){
			$timeout(function(){
				$("#frm-addpunish").form('reset');			
			})
			$('.ui.add_punish').modal({
				autofocus:false,
	         	observeChanges: true,onApprove: function() {
	         		if ( $("#frm-addpunish").form('is valid') ) {
	         			var para = null;
	         			if ($rootScope.strhrtemp != null) {
	         				para = $rootScope.strhrtemp;
	         			}
	         			var ntval = $('#addpunist_seluser').val();
	         			if ( ntval != null) {
	         				if ($.isArray( ntval )) {
								ntval = ntval.join(',');
	         				}
	         				if (para != null) {
	         					para += ',' +ntval;
	         				}else{
	         					para =  ntval;
	         				}
	         			}
	         			$scope.addpunist.seluser = para;
						$scope.addExecute($scope.addpunist);
	         		}else{
	         			return false;
	         		}
	          	}
	        }).modal('show');
		}

		//执行添加
		$scope.addExecute   = function( _d ){
			var _url ='Jiangcheng/addPunish';
			dataFactoryCRM.postDataByAPI( _url , $.param( _d )).then(function(data){
				if(data.rcode == 1){
					$('#successMsg').html(data.msg);
					$('.ui.modalSuccess').modal('show');
					setTimeout(function() {
		                $('.ui.modalSuccess').modal('hide')
		            }, 1000);
		            $scope.getDataList();
				}else{
					$('#failMsg').html(data.msg);
					$('.ui.modalFail').modal('show');
					setTimeout(function() {
		                $('.ui.modalFail').modal('hide')
		            }, 1000);
				}	            
			})
			$rootScope.strhrtemp = null;
		}

		//惩罚删除记录
		$scope.pdelete = function(  ){
			var vuser = this.vlist ;
			//搜索是否最后一条记录
			var _url ='Jiangcheng/chenkOnerecord';
			var _dmsg ='确定要删除记录吗？';
			dataFactoryCRM.postDataByAPI( _url , { fid: vuser['fine_id'] } ).then(function(udata){
				if(udata.rcode == 1){
					_dmsg ='这是当前事件的最后一条记录，确定要删除记录吗？';
				}
				$('#comfirmMsg').html( _dmsg );
				$('.ui.modalComfirm').modal({
					observeChanges: true,onApprove: function() {
						var _url ='Jiangcheng/delPunish';
						dataFactoryCRM.postDataByAPI( _url , { fid: vuser['fine_id'], uid: vuser['sales_id'] } ).then(function(data){
							if(data.rcode == 1){
								$('#successMsg').html(data.msg);
								$('.ui.modalSuccess').modal('show');
								setTimeout(function() {
					                $('.ui.modalSuccess').modal('hide')
					            }, 1000);
					            $scope.getDataList();
							}else{
								$('#failMsg').html(data.msg);
								$('.ui.modalFail').modal('show');
								setTimeout(function() {
					                $('.ui.modalFail').modal('hide')
					            }, 1000);
							}	            
						})
					}
				}).modal('show');
			})
		}

		//编辑惩罚
		$scope.editDetail     = function( cid ){
	        dataFactoryCRM.getDataByAPI('Jiangcheng/editorPunishInfoById?id=' + cid ).then(function(_d){
	        	$timeout(function(){
		        	$scope.editpunist = _d.data;
					$('#editpunist_hrfinetype').dropdown('set selected', _d.data.hr_fine_type);
					$('#editpunist_finetype').dropdown('set selected', _d.data.fine_type);
	        	})
 	        })
			$('.ui.edit_punish').modal({
				autofocus:false,
				onApprove: function() {
					if( $('#frm-editpunish').form('is valid') ){
				  		$scope.editExecute( $scope.editpunist );
					}else{
					     return false;
					}
				}
	        }).modal('show');
		}

		$scope.editExecute = function( _d ){
			var _url ='Jiangcheng/editPunish';
			dataFactoryCRM.postDataByAPI( _url , $.param( _d  ) ).then(function(data){
				if(data.rcode == 1){
					$('#successMsg').html(data.msg);
					$('.ui.modalSuccess').modal('show');
					setTimeout(function() {
		                $('.ui.modalSuccess').modal('hide')
		            }, 1000);
		            $scope.getDataList();
				}else{
					$('#failMsg').html(data.msg);
					$('.ui.modalFail').modal('show');
					setTimeout(function() {
		                $('.ui.modalFail').modal('hide')
		            }, 1000);
				}	            
			})
		}


        /**自动运行 */
        $scope.auto = function() {
            $scope.getSector();

        };
        $scope.auto();

	})
	
})